package de.szut.splasher;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import de.szut.database.DatabaseHelper;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.List;

/**
 * TODO: OUTDATED
 * Created by dzebcuks on 20.08.13.
 */
public class Launcher extends Activity {

    private static final String FIRST_APP_START = "FIRST_APP_START";

    private static final String XML_DATA_FILENAME = "abilities.xml";

    private boolean dev = true;

    private DatabaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main); // Need a launcher layout
        this.helper = DatabaseHelper.getInstance(this);
        if (this.isFirstAppStart() || this.dev) {
//            this.fillDatabase();
        }
//        SVG svg = SVGParser.getSVGFromResource(getResources(),
//                R.raw.test2);
//        ImageView view = (ImageView) this.findViewById(R.id.map);
//        view.setImageDrawable(svg.createPictureDrawable());
    }

    /**
     * Fills the database with the given xml data. Should be only executed once.
//     */
//    private void fillDatabase() {
//        AbilityXmlParser parser = new AbilityXmlParser();
//        List<Object> abilities;
//        try {
//            abilities = parser.parse(this.getResources().getAssets().open(Launcher
//                    .XML_DATA_FILENAME));
////            ModifierStore store = ModifierStore.getInstance(this);
//            //store.initiateDatabase(abilities);
//        } catch (XmlPullParserException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * Checks if the app is started for the first time
     * If so, the preference value of the key is set to false.
     * Used to determine, if the database needs to be upgraded.
     *
     * @return
     */
    private boolean isFirstAppStart() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean firstStart = prefs.getBoolean(Launcher.FIRST_APP_START, true);
        if (firstStart) {
            prefs.edit().putBoolean(Launcher.FIRST_APP_START, false).commit();
        }
        return firstStart;
    }
}
